package com.shaoshaohuo.app.versionupdatelibrary.entity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.shaoshaohuo.app.versionupdatelibrary.Activity.TranslucentActivity;
import com.shaoshaohuo.app.versionupdatelibrary.service.VersionUpdateService;


/**
 * Created by Kun on 2017/5/22.
 * GitHub: https://github.com/AndroidKun
 * CSDN: http://blog.csdn.net/a1533588867
 * Description:
 */

public class VersionUpdateConfig {

    private static VersionUpdateConfig config = new VersionUpdateConfig();
    private FileBean fileBean;
    private Context context;

    private VersionUpdateConfig() {

    }

    public static VersionUpdateConfig getInstance() {
        return config;
    }

    /**
     * 设置上下文
     *
     * @param context
     * @return
     */
    public VersionUpdateConfig setContext(Context context) {
        this.context = context;
        return config;
    }

    /**
     * 设置文件保存路径
     *
     * @param path
     * @return
     */
    public VersionUpdateConfig setFileSavePath(String path) {
        Config.downLoadPath = path;
        return config;
    }

    /**
     * 设置通知标题
     */
    public VersionUpdateConfig setNotificationTitle(String title) {
        Config.notificationTitle = title;
        return config;
    }

    /**
     * 设置通知图标
     */
    public VersionUpdateConfig setNotificationIconRes(int resId) {
        Config.notificaionIconResId = resId;
        return config;
    }

    /**
     * 设置通知小图标
     */
    public VersionUpdateConfig setNotificationSmallIconRes(int resId) {
        Config.notificaionSmallIconResId = resId;
        return config;
    }

    /**
     * 设置下载链接
     *
     * @param url
     * @return
     */
    public VersionUpdateConfig setDownLoadURL(String url) {
        fileBean = new FileBean(0, System.currentTimeMillis() + ".apk", url, 0, 0);
        return config;
    }

    /**
     * 开始下载
     */
    public void startDownLoad() {
        if (context == null) {
            throw new NullPointerException("context cannot be null, you must first call setContext().");
        }
        if (fileBean == null) {
            throw new NullPointerException("url cannot be null, you must first call setDownLoadURL().");
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ((Activity) context).startActivity(new Intent(context, TranslucentActivity.class));
        } else {
            passCheck();
        }
    }

    public void passCheck() {
        Intent startIntent = new Intent(context, VersionUpdateService.class);
        startIntent.setAction(VersionUpdateService.ACTION_START);
        startIntent.putExtra("FileBean", fileBean);
        context.startService(startIntent);
    }

    private Intent updataService;
    private static int REQUESTPERMISSION = 110;

    public void upData() {
        updataService = new Intent(context, TranslucentActivity.class);
//        ((Activity) context).startActivity(new Intent(context, TranslucentActivity.class));
//        updataService.putExtra("downloadurl",version.url);
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//申请权限    
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUESTPERMISSION);
//            CommonToast.showToast("请允许权限进行下载安装");
        } else {
            context.startService(updataService);
        }
    }

//    @Override
//    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        if (requestCode == REQUESTPERMISSION) {
//            if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    if (updataService != null)
//                        startService(updataService);
//                } else {
////提示没有权限，安装不了咯
//                }
//            }
//        }
//    }

}
